package com.charm.information.ui.activity

import android.os.Bundle
import android.support.v4.widget.PopupWindowCompat
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebView
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.charm.codes.imageLoad.XGlide
import com.charm.frameworkarou.share.ShareUtils
import com.charm.information.R
import com.charm.information.adapter.EastDetalCommentAdapter
import com.charm.information.entity.EastCommentEntity
import com.charm.information.entity.EastItemEntity
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_east_head_detail.*
import kotlinx.android.synthetic.main.layout_east_detail_toolbar.*

/**
 * Created by tools on 2018/1/13.
 */
@Route(path = "/information/eastHeadLine/detail")
class EastHeadLineDetailActivity : AppCompatActivity() {

    //    var toolbar: Toolbar? = null
    lateinit var webview: WebView
    lateinit var header: ImageView
    lateinit var author: TextView
    //    lateinit var actionLike: ImageView
//    lateinit var rv: RecyclerView
    var entity: EastItemEntity? = null
    lateinit var headerView: View
    lateinit var adapter: EastDetalCommentAdapter
    //    lateinit var container: RelativeLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_east_head_detail)
        var ty = intent.extras["entity"]
        entity = Gson().fromJson(ty.toString(), EastItemEntity::class.java) as EastItemEntity
        initHeaderView()
        initData(entity!!)
        initListener()
    }

    private fun initHeaderView() {
        headerView = LayoutInflater.from(this@EastHeadLineDetailActivity).inflate(R.layout.layout_east_detail_header, null)
        webview = headerView!!.findViewById(R.id.webview)
        header = headerView!!.findViewById(R.id.header)
        author = headerView!!.findViewById(R.id.author)
        webview.loadUrl(entity!!.url)
        author.text = entity!!.posterScreenName
        XGlide.getInstance().with(this@EastHeadLineDetailActivity)
                .load(entity!!.imageUrls!![0])
                .placeholder(R.drawable.bg_block)
                .error(R.drawable.bg_block)
                .into(header)
                .displayImage()
    }

    private fun initData(t: EastItemEntity) {
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        rv.layoutManager = LinearLayoutManager(this@EastHeadLineDetailActivity)
        fabricateCommentData()
        adapter = EastDetalCommentAdapter(fabricateCommentData())
        adapter.addHeaderView(headerView)
        rv.adapter = adapter
    }

    private fun fabricateCommentData(): ArrayList<EastCommentEntity> {
        var list = ArrayList<EastCommentEntity>()
        (0..9).mapTo(list) { EastCommentEntity("名字" + it, "回复了内容" + it, it) }
        return list
    }

    private fun initListener() {
        toolbar!!.setNavigationOnClickListener {
            super.onBackPressed()
        }
        action_like.setOnClickListener {
            ARouter.getInstance().build("/logininfo/login")
                    .withInt("revealWidth", container.width)
                    .withInt("revealHeight", container.height)
                    .navigation()
        }
        action_share.setOnClickListener {
            ShareUtils.share(supportFragmentManager)
        }
    }
}